package com.bianguo.android.bprinter.utils;

import android.os.Handler;
import android.os.Looper;

import com.bianguo.android.baselibrary.utils.MLog;

import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbSession;

public class SmbManager {

    private static volatile SmbManager instance;
    private ExecutorService executorService;
    private Handler mHandler;
    private SmbListener smbListener;

    private SmbManager() {
        executorService = Executors.newCachedThreadPool();
        mHandler = new Handler(Looper.myLooper());
    }

    public static SmbManager getInstance() {
        SmbManager manager = instance;
        if (manager == null) {
            synchronized (SmbManager.class) {
                if (manager == null) {
                    manager = new SmbManager();
                    instance = manager;
                }
            }
        }
        return instance;
    }

    public void scaleShareFiles(String ip) {
        executorService.execute(() -> {
            UniAddress mDomain = null;
            try {
                //登录授权
                mDomain = UniAddress.getByName(ip);
                NtlmPasswordAuthentication mAuthentication = new NtlmPasswordAuthentication(null, null, null);
                SmbSession.logon(mDomain, mAuthentication);
                //登录授权结束
                String rootPath = "smb://" + ip + "/";
                SmbFile mRootFolder;
                try {
                    mRootFolder = new SmbFile(rootPath, mAuthentication);
                    try {
                        SmbFile[] files = mRootFolder.listFiles();
                        ArrayList<SmbFile> smbFiles = new ArrayList<>();
                        Collections.addAll(smbFiles, files);
                        mHandler.post(() -> {
                            if (smbListener != null) {
                                smbListener.showScaleShareFiles(smbFiles);
                            }
                        });
                    } catch (SmbException e) {
                        // ...
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (SmbException e) {
                e.printStackTrace();
            }
        });
    }

    public void doFile(SmbFile smbFile) {
        executorService.execute(() -> {
            try {
                MLog.mLog("canRead = " + smbFile.canRead() + ", canWrite = " + smbFile.canWrite());
                SmbFile[] smbFiles = smbFile.listFiles();
                MLog.mLog("length = " + smbFiles.length);
            } catch (SmbException e) {
                e.printStackTrace();
                MLog.mLog("smb error : " + e.getMessage());
            }
        });
    }

    public void setSmbListener(SmbListener smbListener) {
        this.smbListener = smbListener;
    }

    public interface SmbListener {

        void showScaleShareFiles(ArrayList<SmbFile> smbFiles);
    }
}
